<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:p="http://www.w3.org/ns/xproc"
                xmlns:c="http://www.w3.org/ns/xproc-step"
                xmlns:cx="http://xmlcalabash.com/ns/extensions"
                version="2.0">

   <xsl:output indent="yes"/>

   <!-- must end with a slash, e.g. http://localhost:8080/qizx/xqs/package/ -->
   <xsl:param name="package-uri" as="xs:string"/>
   <xsl:param name="lib-ns"      as="xs:string"/>
   <xsl:param name="lib-prefix"  as="xs:string" select="'ns'"/>

   <xsl:template match="services">
      <p:library version="1.0">
         <xsl:namespace name="{ $lib-prefix }" select="$lib-ns"/>
         <xsl:apply-templates/>
      </p:library>
   </xsl:template>

   <xsl:template match="service">
      <!-- and output-option? -->
      <xsl:apply-templates select="documentation"/>
      <p:declare-step type="{ $lib-prefix }:{ @name }" name="{ @name }">
         <p:input port="qizx" kind="parameter"/>
         <xsl:apply-templates select="parameter"/>
         <p:output port="result"/>
         <p:variable name="uri-base" select="'{ $package-uri }{ @name }'"/>
         <p:variable name="params">
            <xsl:attribute name="select">
               <xsl:text>string-join((</xsl:text>
               <xsl:value-of separator=", " select="
                   parameter/concat('concat(''', @name, '='', $', @name, ')[$', @name, ']')"/>
               <xsl:text>), '&amp;')</xsl:text>
            </xsl:attribute>
         </p:variable>
         <p:parameters name="qizx-params">
            <p:input port="parameters">
               <p:pipe port="qizx" step="{ @name }"/>
            </p:input>
         </p:parameters>
         <p:identity>
            <p:input port="source">
               <p:inline>
                  <c:request method="get" auth-method="basic"/>
               </p:inline>
            </p:input>
         </p:identity>
         <p:add-attribute attribute-name="username" match="c:request">
            <p:with-option name="attribute-value" select="c:param-set/c:param[@name eq 'username']/@value">
               <p:pipe port="result" step="qizx-params"/>
            </p:with-option>
         </p:add-attribute>
         <p:add-attribute attribute-name="password" match="c:request">
            <p:with-option name="attribute-value" select="c:param-set/c:param[@name eq 'password']/@value">
               <p:pipe port="result" step="qizx-params"/>
            </p:with-option>
         </p:add-attribute>
         <p:add-attribute attribute-name="href" match="c:request">
            <p:with-option name="attribute-value" select="concat($uri-base, '?'[$params], $params)"/>
         </p:add-attribute>
         <p:http-request/>
         <xsl:apply-templates select="output-option"/>
      </p:declare-step>
   </xsl:template>

   <xsl:template match="parameter">
      <!-- cx:type not used for now, it does not support sequence occurence indicators -->
      <!--p:option name="{ @name }" select="()" cx:type="{ @type }"/-->
      <p:option name="{ @name }" select="()"/>
   </xsl:template>

   <xsl:template match="documentation">
      <xsl:comment>
         <xsl:value-of select="."/>
      </xsl:comment>
   </xsl:template>

   <xsl:template match="output-option" priority="-1"/>

   <xsl:template match="output-option[@name eq 'method'][. eq 'html']">
      <p:unescape-markup content-type="text/html"/>
      <p:unwrap match="c:body"/>
   </xsl:template>

</xsl:stylesheet>
